package cn.itcast.service;

import cn.itcast.pojo.PayLog;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.wxpay.sdk.WXPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WxPayService {

    @Autowired
    private WXPay wxPay;

    @Reference
    private OrderService orderService;


    @Reference
    private PayLogService payLogService;

    //返回微信支付生成的二维码url
    public String getUrl(String id) {
        String url = null;
        Map<String, String> data = new HashMap<String, String>();

        data.put("body", "传智健康订单支付");
        data.put("out_trade_no", id);
        data.put("device_info", "13467007045764");
        data.put("fee_type", "CNY");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://zhangzhe.free.idcfengye.com/pay/notify");
        data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
        data.put("product_id", "12");
        data.put("mch_id", "1230000109");
        data.put("nonce_str", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS");

        try {
            //调用方法统一下单
            Map<String, String> resp = wxPay.unifiedOrder(data);
            System.out.println(resp);
            url = resp.get("code_url");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //创建支付日志
        PayLog payLog = new PayLog();
        payLog.setOrderId(id); //订单id
        payLog.setPayStatus(0); //支付状态
        //调用api保存
        payLogService.save(payLog);

        return url;
    }
}
